package com.kzj.fun.container;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;

public class MagnifyingGlass extends JPanel {
    private Point center;
    private int range;
    private int ratio;
    private BufferedImage image;
    private ImageObserver observer;
    private final int distance=32;
    private final int textHeight=60;
    public MagnifyingGlass(Point center,int range,int ratio,BufferedImage image,ImageObserver observer){
        this.center=center;
        this.range=range;
        this.ratio=ratio;
        this.image=image;
        this.observer=observer;
        int width=(2 * range + 1)*ratio;
        int height=(2 * range + 1)*ratio+textHeight;
            this.setBackground(new Color(27,174,255,100));
        this.setSize(width,height);
    }
    public void update(Point point,BufferedImage image){
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            this.center=point;
            this.image=image;
            int x=center.x+distance;
            int y=center.y+distance;
            if(x>screenSize.width-getWidth()){
                x=center.x-getWidth()-distance;
            }
            if(y>screenSize.height-getHeight()){
                y=center.y-getHeight()-distance;
            }
            setLocation(x,y);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        draw(g);
    }

    public void draw(Graphics g){
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            int subX=center.x-range;
            int subY=center.y-range;
            int subWidth=range*2+1;
            int subHeight=range*2+1;
            int centX=getWidth()/2-ratio/2;
            int centY=getWidth()/2-ratio/2;
            if(subX<0){
                subX=0;
                centX=center.x*ratio;
            }else if(subX>screenSize.width-subWidth){
                subX=screenSize.width-getWidth();
                centX=getWidth()-(screenSize.width-center.x)*ratio;
            }
            if(subY<0){
                subY=0;
                centY=center.y*ratio;
            }else if(subY>screenSize.height-subHeight){
                subY=screenSize.height-getWidth();
                centY=getHeight()-(screenSize.height-center.y)*ratio;
            }
            BufferedImage subimage = image.getSubimage(subX,subY,subWidth,subHeight);
            g.drawImage(subimage,0,0,getWidth(),getWidth(),observer);
            g.setColor(Color.WHITE);
            g.drawRect(0,0,getWidth()-1,getWidth());
            g.drawRect(centX,centY,ratio,ratio);
            g.drawRect(0,getWidth(),getWidth()-1,textHeight-1);
            int fontSize=12;
            int fontInterval=5;
            int rightInterval=10;
            g.setColor(Color.BLACK);
            g.setFont( new Font("Serif", Font.PLAIN, fontSize));


            int rgb = image.getRGB(center.x, center.y);
            // 解析 ARGB 值
            int alpha = (rgb >> 24) & 0xFF; // Alpha
            int red = (rgb >> 16) & 0xFF;   // Red
            int green = (rgb >> 8) & 0xFF;  // Green
            int blue = (rgb) & 0xFF;        // Blue

            g.drawString(String.format("颜色( a , r , g , b ) : ( %d , %d , %d , %d )",alpha,red,green,blue),rightInterval,getWidth()+fontSize+fontInterval);
            g.drawString(String.format("坐标( x , y ) : ( %d , %d )",center.x,center.y),rightInterval,getWidth()+(fontSize+fontInterval)*2);
            g.drawString("CTRL + C 复制颜色",rightInterval,getWidth()+(fontSize+fontInterval)*3);
    }
}
